class Calculator:
    """计算器类，提供基本和高级的数学运算功能"""
    
    def __init__(self):
        """初始化计算器，设置结果为0并创建历史记录列表"""
        self.result = 0
        self.history = []
        self.memory = 0
    
    def add(self, num):
        """加法运算"""
        self.result += num
        self.history.append(f"加 {num}")
        return self.result
    
    def subtract(self, num):
        """减法运算"""
        self.result -= num
        self.history.append(f"减 {num}")
        return self.result
    
    def multiply(self, num):
        """乘法运算"""
        self.result *= num
        self.history.append(f"乘 {num}")
        return self.result
    
    def divide(self, num):
        """除法运算"""
        if num == 0:
            raise ValueError("除数不能为零")
        self.result /= num
        self.history.append(f"除以 {num}")
        return self.result
    
    def power(self, num):
        """幂运算"""
        self.result **= num
        self.history.append(f"的 {num} 次方")
        return self.result
    
    def square_root(self):
        """平方根运算"""
        if self.result < 0:
            raise ValueError("负数没有实数平方根")
        self.result = self.result ** 0.5
        self.history.append("的平方根")
        return self.result
    
    def clear(self):
        """清除当前结果"""
        self.result = 0
        self.history.append("清除")
        return self.result
    
    def set_value(self, num):
        """设置结果值"""
        self.result = num
        self.history.append(f"设置为 {num}")
        return self.result
    
    def memory_store(self):
        """将当前结果存储到内存"""
        self.memory = self.result
        self.history.append("存储到内存")
        return self.memory
    
    def memory_recall(self):
        """从内存调用值"""
        self.result = self.memory
        self.history.append("从内存调用")
        return self.result
    
    def memory_clear(self):
        """清除内存"""
        self.memory = 0
        self.history.append("清除内存")
        return self.memory
    
    def get_result(self):
        """获取当前结果"""
        return self.result
    
    def get_history(self):
        """获取计算历史"""
        return self.history


def calculator_interface():
    """计算器的交互式界面"""
    calc = Calculator()
    print("欢迎使用Python计算器")
    print("可用命令：")
    print("set X - 设置值为X")
    print("add X - 加X")
    print("sub X - 减X")
    print("mul X - 乘X")
    print("div X - 除以X")
    print("pow X - X次方")
    print("sqrt - 平方根")
    print("clear - 清除结果")
    print("ms - 存储到内存")
    print("mr - 从内存调用")
    print("mc - 清除内存")
    print("history - 显示历史记录")
    print("exit - 退出计算器")
    
    while True:
        print(f"当前结果: {calc.get_result()}")
        command = input("请输入命令: ").strip().lower()
        
        if command == "exit":
            print("谢谢使用，再见!")
            break
        elif command == "sqrt":
            try:
                calc.square_root()
            except ValueError as e:
                print(f"错误: {e}")
        elif command == "clear":
            calc.clear()
        elif command == "ms":
            calc.memory_store()
        elif command == "mr":
            calc.memory_recall()
        elif command == "mc":
            calc.memory_clear()
        elif command == "history":
            for i, action in enumerate(calc.get_history()):
                print(f"{i+1}: {action}")
        else:
            parts = command.split()
            if len(parts) == 2:
                cmd, value = parts
                try:
                    num = float(value)
                    if cmd == "add":
                        calc.add(num)
                    elif cmd == "sub":
                        calc.subtract(num)
                    elif cmd == "mul":
                        calc.multiply(num)
                    elif cmd == "div":
                        try:
                            calc.divide(num)
                        except ValueError as e:
                            print(f"错误: {e}")
                    elif cmd == "pow":
                        calc.power(num)
                    elif cmd == "set":
                        calc.set_value(num)
                    else:
                        print("未知命令")
                except ValueError:
                    print("请输入有效的数字")
            else:
                print("命令格式不正确")


if __name__ == "__main__":
    calculator_interface()